package com.zhentao.service.impl;

import cn.hutool.core.lang.UUID;
import com.zhentao.image.ImageUploadRequest;
import com.zhentao.image.UploadResponse;

import com.zhentao.minio.service.impl.MinioFileStorageServiceImpl;
import com.zhentao.service.FileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.Base64Utils;
import com.zhentao.image.UploadResponse;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.TimeUnit;

import org.springframework.util.StringUtils;


import static com.zhentao.common.Constans.*;

import static com.zhentao.common.Constans.VEDIO_PREFIX;
import static com.zhentao.common.Constans.VEDIO_PREFIX_HAND;

/**
 * @ClassName: FileUploadServiceImpl
 * @Author: 振涛教育_Q
 * @Date: 2024/12/6 13:57
 */
@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    private MinioFileStorageServiceImpl minioFileStorageService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * @description 上传视频文件
     * @author 振涛教育_小Q
     * @date 2024/12/6 13:59
     * @param file
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     */
    // 根据需要自定义文件保存目录
    private static final String UPLOAD_DIR = "uploads/";
    @Override
    public ResponseEntity<String> uploadVideo(MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseEntity.badRequest().body("文件不能为空");
            }
            // 获取文件的原始名称并清理路径
            String fileName = StringUtils.cleanPath(file.getOriginalFilename());

            // 构建目标文件的位置
            Path targetLocation = Paths.get(UPLOAD_DIR + fileName);

            // 使用 REPLACE_EXISTING 选项来覆盖已存在的文件
            Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);

            // 上传值minio
            String prfix = UUID.randomUUID().toString(true);
            String path = minioFileStorageService.uploadVedioFile(VEDIO_PREFIX, prfix + ".webm", file.getInputStream());
            // TODO  要返回的路径字符串 逻辑
            redisTemplate.opsForValue().set(VEDIO_PREFIX_REDIS,path);
            redisTemplate.expire(VEDIO_PREFIX_REDIS,VEDIO_HAND_EXPIRE, TimeUnit.SECONDS);

            return ResponseEntity.ok("上传成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @description 手绘上传图片文件
     * @author 振涛教育_小Q
     * @date 2024/12/6 13:59
     * @param request
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     */
    @Override
    public ResponseEntity<?> uploadImage(ImageUploadRequest request) {
        try {
            // 获取 Base64 编码的图片数据
            String base64Image = request.getImage();

            // 解码 Base64 图片数据
            byte[] imageBytes = Base64Utils.decodeFromString(base64Image);

            // 保存图片到文件
            String filePath = "uploads/drawing.png";
            try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
                fileOutputStream.write(imageBytes);
            }
            // 上传到minio
            String uuid = UUID.randomUUID().toString(true);
            FileInputStream fileInputStream = new FileInputStream(filePath);

            String path = minioFileStorageService.uploadImgFile(VEDIO_PREFIX_HAND, uuid + ".jpg", fileInputStream);
            // 加入在redis中
            redisTemplate.opsForValue().set(HAND_PREFIX_REDIS, path);
            redisTemplate.expire(HAND_PREFIX_REDIS, VEDIO_HAND_EXPIRE, TimeUnit.SECONDS);
            // 返回上传成功响应
            return ResponseEntity.status(200).body(new UploadResponse("图片上传成功", filePath));
        } catch (IOException e) {
            // 返回上传失败响应
            return ResponseEntity.status(500).body(new UploadResponse("图片上传失败", null));
        }
    }


}
